package org.example.controller;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.SneakyThrows;
import org.example.entity.QuestionImportDTO;
import org.example.entity.QuestionInfoEntity;
import org.example.service.QuestionInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;


@RestController
@RequestMapping("api/questions")
public class QuestionInfoController {

    @Autowired
    private QuestionInfoService questionInfoService;


    @GetMapping("/exam/{examId}")
    public List<QuestionInfoEntity> getQuestionsByExamId(@PathVariable Integer examId) {
        return questionInfoService.getQuestionsByExamId(examId);
    }

    /**
     * 添加题目
     *
     * @param question 题目实体
     * @return 添加后的题目
     */
    @PostMapping("/addQuestion")
    public QuestionInfoEntity addQuestion(@RequestBody QuestionInfoEntity question) {
        return questionInfoService.addQuestion(question);
    }

    /**
     * 根据ID删除题目
     *
     * @param questionId 题目ID
     * @return 是否删除成功
     */
    @DeleteMapping("/{id}")
    public boolean deleteQuestionById(@PathVariable("id") Long questionId) {
        return questionInfoService.deleteQuestionById(questionId);
    }

    @DeleteMapping("/remove-duplicates/{examId}")
    public String removeDuplicates(@PathVariable Integer examId) {
        questionInfoService.removeDuplicates(examId); //
        return "Duplicates removed successfully for exam ID: " + examId;
    }

    /**
     * 更新题目
     *
     * @param question 题目实体
     * @return 更新后的题目
     */
    @PutMapping("/update")
    public QuestionInfoEntity updateQuestion(@RequestBody QuestionInfoEntity question) {
        return questionInfoService.updateQuestion(question);
    }

    /**
     * 根据ID获取题目
     *
     * @param questionId 题目ID
     * @return 对应的题目
     */
    @GetMapping("/id/{id}")
    public QuestionInfoEntity getQuestionById(@PathVariable("id") Long questionId) {
        return questionInfoService.getQuestionById(questionId);
    }

    /**
     * 分页获取所有题目
     *
     * @param page 当前页
     * @param size 每页条数
     * @return 分页数据
     */
    @GetMapping("/page")
    public IPage<QuestionInfoEntity> getAllQuestions(@RequestParam(defaultValue = "1") int page,
                                                     @RequestParam(defaultValue = "10") int size) {
        Page<QuestionInfoEntity> queryPage = new Page<>(page, size);
        return questionInfoService.getAllQuestions(queryPage);
    }

    /**
     * 根据科目搜索题目（分页）
     *
     * @param subject 科目
     * @param page    当前页
     * @param size    每页条数
     * @return 分页数据
     */
    @GetMapping("/search/subject")
    public IPage<QuestionInfoEntity> searchQuestionsBySubject(@RequestParam String subject,
                                                              @RequestParam(defaultValue = "1") int page,
                                                              @RequestParam(defaultValue = "10") int size) {
        Page<QuestionInfoEntity> queryPage = new Page<>(page, size);
        return questionInfoService.searchQuestionsBySubject(subject, queryPage);
    }

    /**
     * 根据知识点和题目类型搜索题目（分页）
     *
     * @param knowledge 知识点
     * @param type      题目类型
     * @param page      当前页
     * @param size      每页条数
     * @return 分页数据
     */
    @GetMapping("/search/knowledge")
    public IPage<QuestionInfoEntity> searchQuestionsByKnowledgeAndType(@RequestParam String knowledge,
                                                                       @RequestParam String type,
                                                                       @RequestParam(defaultValue = "1") int page,
                                                                       @RequestParam(defaultValue = "10") int size) {
        Page<QuestionInfoEntity> queryPage = new Page<>(page, size);
        return questionInfoService.searchQuestionsByKnowledgeAndType(knowledge, type, queryPage);
    }

    /**
     * 根据题目难度搜索题目（分页）
     *
     * @param difficulty 难度
     * @param page       当前页
     * @param size       每页条数
     * @return 分页数据
     */
    @GetMapping("/search/difficulty")
    public IPage<QuestionInfoEntity> searchQuestionsByDifficulty(@RequestParam byte difficulty,
                                                                 @RequestParam(defaultValue = "1") int page,
                                                                 @RequestParam(defaultValue = "10") int size) {
        Page<QuestionInfoEntity> queryPage = new Page<>(page, size);
        return questionInfoService.searchQuestionsByDifficulty(difficulty, queryPage);
    }

    @SneakyThrows
    @PostMapping("/import")
    public String importQuestions(@RequestParam("file") MultipartFile file) {

        List<QuestionImportDTO> questionList = EasyExcel.read(file.getInputStream())
                .head(QuestionImportDTO.class)
                .sheet()
                .doReadSync();

        questionInfoService.importQuestions(questionList);
        return "批量导入成功！";

    }
}
